<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/25
 * Time: 17:45
 */
namespace app\common\model;

use OSS\OssClient;
use OSS\Core\OssException;
use think\Image;
use think\facade\Config;
use app\common\controller\Base;




class uploadUtil{

    /*public function  uploadUtil(Request $request){
        if($request->isPost()){
            $file = $request->file('filed');
            $accessKeyId = "LTAI4FpYnf";
            $accessKeySecret = "K9rN5h";
            // Endpoint以杭州为例，其它Region请按实际情况填写。
            $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
            // 存储空间名称
            //https://help.aliyun.com/document_detail/47735.html?spm=a2c4g.11186623.6.1090.29fa1329s237Sx
            $bucket= "abing9";
            $info = $file->getInfo();
            $object = rand(1000,9999).'.png';  //文件名
            $content = $info['tmp_name'];//文件内容
            try {
                $ossClinet = new OssClient($accessKeyId,$accessKeySecret,$endpoint);
                $res = $ossClinet->uploadFile($bucket,$object,$content);
                print_r($res['info']['url']);
            }catch (OssException $exception){
                print_r($exception);
            }


        }else{
            return $this->fetch();
        }
    }*/




    /* OSS图片上传
    * liwei
    * */
   /* public function oss_uploadImage()
    {
        if(request()->has('base64', 'post')){
            $data = $_POST['base64'];
            $result = $this->new_base64_upload($data);
            if ($result['code'] === 200){
                $fileResult = &$result['data'];
                $filePath = $fileResult['path'] . $fileResult['name'];
                $ossFileName = implode('/', ['upload/image',date('Ymd'),$fileResult['name']]);
                try {
                    //$configs =  new \app\extend\aliyun-oss\samples;
                    $config = config('aliyun_oss');
                    //实例化对象 将配置传入
                    $ossClient = new OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
                    $result = $ossClient->uploadFile($config['Bucket'], $ossFileName, $filePath);
                    $arr = [
                        'oss_url' => $result['info']['url'],  //上传资源地址
                        'relative_path' => $ossFileName     //数据库保存名称(相对路径)
                    ];
                } catch (OssException $e) {
                    return $e->getMessage();
                }finally {
                    unlink($filePath);
                }
                $this->jsonReturn(0, '成功oss', array('file' => $arr['oss_url']));
            }
            $this->jsonReturn(400, $result['msg']);
        }else{
            /*获取到上传的文件
            //$file = request()->file('picture');
            $file = $_FILES['picture'];
            if ($file) {
                $name = $file['name'];
                $format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
                /*判断图片格式
                $allow_type = ['.jpg', '.jpeg', '.gif', '.bmp', '.png'];
                if (! in_array($format, $allow_type)) {
                    $this->jsonReturn(400, "文件格式不在允许范围内哦");

                }
                // 尝试执行
                try {
                   // $keyid =Config::get("KeyId.");
                  //  $keysecret =Config::get("jYDY5nvtRdMn8tMH3slcOdQqs3C2U9.");
                    ////$endpoint =Config::get("oss-cn-shenzhen.aliyuncs.com.");
                   // $bucket =Config::get("zjhcloud.oss-cn-shenzhen.aliyuncs.com.");
                    // config('aliyun_oss');
                    $keyid    = "LTAIqe9k1kpr5gQT";  //您的Access Key ID
                    $keysecret  = 'iwGDzruuECj2FZdPUhTqlFuNlLdruT';  //您的Access Key Secret
                    $endpoint   = "oss-cn-shenzhen.aliyuncs.com";  //oss endpoint
                    $bucket     = 'qianxunzhimei';   //Bucket名称
                    //实例化对象 将配置传入
                   // $ossClient = new OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
                    $ossClient = new OssClient($keyid, $keysecret, $endpoint);
                    //这里是有sha1加密 生成文件名 之后连接上后缀
                    $fileName = 'uplaod/image/' . date("Ymd") . '/' . sha1(date('YmdHis', time()) . uniqid()) . $format;
                    //执行阿里云上传
                    $result = $ossClient->uploadFile($bucket, $fileName, $file['tmp_name']);
                    /*组合返回数据
                    $arr = [
                        'oss_url' => $result['info']['url'],  //上传资源地址
                        'relative_path' => $fileName     //数据库保存名称(相对路径)
                    ];
                } catch (OssException $e) {
                    // return  $e->getMessage();
                    return 'error';
                    //return array('status'=> 'error','msg' => $e->getMessage());
                }
                return $arr['oss_url'];
               // return array('status'=> 'success','file' => $arr['oss_url']);
                //return $this->su
                //将结果返回
               // $this->jsonReturn(0, '成功上传到oss', array('file' => $arr['oss_url']));
            }
            return 'error';
            //$this->jsonReturn(400, "文件不存在");
        }
    }*/
    /* OSS图片上传
    * liwei
    * */
    public function oss_uploadImage()
    {
        if(request()->has('base64', 'post')){

        }else{
            /*获取到上传的文件*/
            //$file = request()->file('picture');
            $file = $_FILES['picture'];
            if ($file) {
                $name =  $file['name'];
                $format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
                /*判断图片格式*/
                $allow_type = ['.jpg', '.jpeg', '.gif', '.bmp', '.png'];
                if (! in_array($format, $allow_type)) {
                    $this->jsonReturn(400, "文件格式不在允许范围内哦");

                }
                // 尝试执行
                try {
                    $keyid    = "LTAIqe9k1kpr5gQT";  //您的Access Key ID
                    $keysecret  = 'iwGDzruuECj2FZdPUhTqlFuNlLdruT';  //您的Access Key Secret
                    $endpoint   = "oss-cn-shenzhen.aliyuncs.com";  //oss endpoint
                    $bucket     = 'qianxunzhimei';   //Bucket名称
                    //实例化对象 将配置传入
                    // $ossClient = new OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
                    $ossClient = new OssClient($keyid, $keysecret, $endpoint);
                    //这里是有sha1加密 生成文件名 之后连接上后缀
                    $fileName = 'uplaod/image/' . date("Ymd") . '/' . sha1(date('YmdHis', time()) . uniqid()) . $format;
                    //tmpfile();
                    //执行阿里云上传
                    $result = $ossClient->uploadFile($bucket, $fileName, $file['tmp_name']);
                    /*组合返回数据*/
                    $arr = [
                        'oss_url' => $result['info']['url'],  //上传资源地址
                        'relative_path' => $fileName     //数据库保存名称(相对路径)
                    ];
                } catch (OssException $e) {
                    // return  $e->getMessage();
                    return 'error';
                    //return array('status'=> 'error','msg' => $e->getMessage());
                }
                return $arr['oss_url'];
                // return array('status'=> 'success','file' => $arr['oss_url']);
                //return $this->su
                //将结果返回
                // $this->jsonReturn(0, '成功上传到oss', array('file' => $arr['oss_url']));
            }
            return 'error';
            //$this->jsonReturn(400, "文件不存在");
        }
    }
}